Function.prototype.myCall = function (context) {
    if (typeof this !== "function") {
        throw new Error("Type error")
    }
    let args = [...arguments].slice(1)
    let result = null
    context = context || window
    context.fn = this
    result = context.fn(...args)
    delete context.fn
    return result
}
Function.prototype.myApply = function (context) {
    if (typeof this !== "function") {
        throw new Error("Type error");
    }
    let result = null;
    context = context || window;
    // 与上面代码相比，我们使用 Symbol 来保证属性唯一
    // 也就是保证不会重写用户自己原来定义在 context 中的同名属性
    const fnSymbol = Symbol();
    context[fnSymbol] = this;
    // 执行要被调用的方法
    if (arguments[1]) {
        result = context[fnSymbol](...arguments[1]);
    } else {
        result = context[fnSymbol]();
    }
    delete context[fnSymbol];
    return result;
};
Function.prototype.myBind = function (context) {
    // 判断调用对象是否为函数
    if (typeof this !== "function") {
        throw new Error("Type error");
    }
    // 获取参数
    const args = [...arguments].slice(1)
    const fn = this;
    return function Fn() {
        return fn.apply(
            this instanceof Fn ? this : context,
            // 当前的这个 arguments 是指 Fn 的参数
            args.concat(...arguments)
        );
    };
};
function myNew(context) {
    const obj = new Object();
    obj.__proto__ = context.prototype;
    const res = context.apply(obj, [...arguments].slice(1));
    return typeof res === "object" ? res : obj;
};

